{extend name="common/index" /}

{block name="title"}
<script type="text/html">
</script>

<title>{$title}</title>{/block}

{block name="action"}
{if (!empty($create_url))}
    <div class="layui-input-inline pull-right">
        <a data-href="{$create_url}" data-title="新增" admin-event="formLayer" refresh="" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" style="margin-top:6px;">
            新增
        </a>
    </div>
{/if}


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="huanBang">换绑手机号</a>
    {{#  if(d.status==1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="fenJin">封禁</a>
    {{#  }else{ }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="jieFenJin">解除封禁</a>
    {{#  } }}
    {{#  if(d.is_migration!==0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="qianYi">迁移主播</a>
    {{#  } }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="qianYiLog">迁移记录</a>
     <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="zhuXiao">注销</a>
     {{#  if(d.crm_url){ }}
     <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="inHouTai" data-url="{{d.crm_url}}/unAuthorizedLogin/{{d.crm_name}}/{{d.crm_pwd}}">进入后台</a>
    {{#  } }}
</script>
{/block}

{block name="footer"}
<script type="text/javascript">

    //初始化参数
    var cols = init_cols({$cols|json_encode|raw});
    var list_url = '{$list_url}';
    var create_url = '{$create_url?:""}';
    var huanBang_url= '{$huanBang_url?:""}';
    var fenJin_url='{$fenJin_url?:""}'
    var qianYi_url='{$qianYi_url?:""}'
    var qianYiLog_url='{$qianYiLog_url?:""}'
    var zhuXiao_url='{$zhuXiao_url?:""}'

    var partition_id= '{$partition_id}';
    //初始化表格字段
    function init_cols(cols){
        var arr = [];
        cols.forEach((val) => {
            if(val.type == 'button'){
                val.templet = function (d) {
                    var str = '';
                    d.LAY_COL.option.forEach((v) => {
                        if(v[0] == d[d.LAY_COL['field']]){
                            var color = 'layui-btn-primary';
                            if(v[2] == ''){
                                color = '';
                            }else if(v[2] != undefined){
                                color = 'layui-btn-'+v[2];
                            }
                            str = '<button type="button" class="layui-btn layui-btn-xs '+color+'">'+v[1]+'</button>';
                        }
                    })
                    return str;
                }
            }else if(val.type == 'img'){
                val.templet = function (d) {
                    var str = '';
                    d[d.LAY_COL['field']].split(',').forEach((elem, index) => {
                        if(elem != ''){
                            str += '<img style="padding-left: 10px;" src="'+elem+'" class="view_cp" width="auto" height="100%">';
                        }
                    });
                    return str;
                }
            }
            arr.push(val);
        });
        return cols;
    }

    layui.config({
        base: '/vendor/layui-admin/layui/modules/js/' //静态资源所在路径
    });

    layui.define(['table','form','admin','laydate'], function(exports) {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var table_filter = {};

            table.render({
                elem: '#table'
                , url: list_url
                , cols: [cols]
                , page: true
                , limit: 20
                , height: 'full-180'
                ,toolbar: true //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , text: {
                    none: '无数据' //默认：无数据。
                }
                , even: true
                ,where: {
                    filter:table_filter
                }
            });
            //tool 事件监听
            table.on('tool(table)', function(obj){
                var data = obj.data;
                //注销
                if(obj.event === 'zhuXiao'){
                    layer.confirm('真的注销么，注销后数据不可恢复', function(index){
                        layer.close(index);
                        layer.prompt({title: '请输入管理员密码', formType: 1}, function(pass, index){
                            $.ajax({
                                url: '/backmanage/config/check_admin'
                                ,data: {"code":pass}
                                ,type: 'POST'
                                ,success(res){
                                    if (res.code){
                                        $.ajax({
                                            url: zhuXiao_url+'?id='+data.id
                                            ,data: {"id":data.id}
                                            ,type: 'DELETE'
                                            ,success(res){
                                                if (res.code == 0){
                                                    table.reload("table");
                                                    layer.msg('删除成功');
                                                } else{
                                                    layer.msg('删除失败');
                                                }
                                            }
                                        });
                                        layer.close(index);
                                    } else{
                                        layer.msg('密码错误');
                                    }
                                }
                            });
                            layer.close(index);
                        });
                    });
                }

                //封禁
                if(obj.event === 'fenJin'){
                     $.ajax({
                        url: fenJin_url
                        ,data: {"id":data.id,"status":3}
                        ,type: 'POST'
                        ,success(res){
                            if(typeof  res == "string"){
                                res = JSON.parse(res);
                            }
                            table.reload("table");
                            layer.msg(res.msg);
                        }
                    });
                }

                //解除封禁
                if(obj.event === 'jieFenJin'){
                     $.ajax({
                        url: fenJin_url
                        ,data: {"id":data.id,"status":1}
                        ,type: 'POST'
                        ,success(res){
                            if(typeof  res == "string"){
                                res = JSON.parse(res);
                            }
                            table.reload("table");
                            layer.msg(res.msg);
                        }
                    });
                }

                //换绑
                if(obj.event === 'huanBang'){
                    layer.open({
                        type: 2,
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ["80%", "80%"],
                        content: huanBang_url+'?mobile='+data.mobile,
                        end: function () {
                            //更新表格
                            $("#layui-icon-refresh").click();
                        }
                    });
                }

                //迁移主播
                if(obj.event === 'qianYi'){
                    layer.open({
                        type: 2,
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ["80%", "80%"],
                        content: qianYi_url+'?id='+data.id,
                        end: function () {
                            //更新表格
                            $("#layui-icon-refresh").click();
                        }
                    });
                }

                //主播迁移记录
                if(obj.event === 'qianYiLog'){
                    layer.open({
                        type: 2,
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ["80%", "80%"],
                        content: qianYiLog_url+'?anchor_platform_id='+data.anchor_platform_id,
                        end: function () {
                            //更新表格
                            $("#layui-icon-refresh").click();
                        }
                    });
                }
                
                // 进入后台事件处理
                if(obj.event === 'inHouTai'){
                    // 构建动态URL
                    var backendUrl = data.crm_url + '/unAuthorizedLogin/' + data.crm_name + '/' + data.crm_pwd;
                    window.open(backendUrl, '_blank');
                }
            });
            //筛选框监听
            form.on('submit(search)', function(data){
                table_filter = data.field;
                reload();
                return false;
            });
            //重载 刷新
            $('#layui-icon-refresh').click(function () {
                reload();
            });
            //重载 刷新
            function reload(){
                table.reload('table',{
                    where: {
                        filter:table_filter
                    }
                });
            }
            exports('level', {});

            $(document).on('click','.view_cp',function(){
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    area: 'auto',
                    content: '<img style="height:500px;" src="'+$(this).attr('src')+'">'
                });
            });
            //日期时间范围
            laydate.render({
                elem: '#time_start'
                ,type: 'datetime'
            });
            laydate.render({
                elem: '#time_end'
                ,type: 'datetime'
            });

        });



</script>

{/block}
